/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/* 
 * Copyright (c) 2009 City University of Hong Kong
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License m_version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Author: Wilson Thong (wilsonwk@ee.cityu.edu.hk) 
 */
#ifndef TRAFFIC_ALLOC_HELPER
#define TRAFFIC_ALLOC_HELPER

#include "jitter-sim-include.h"
#include "base-topology.h"
#include "unicast-session-helper.h"
#include "ns3/random-variable.h"
#include "ns3/data-rate.h"
#include <list>

namespace ns3 {
namespace eecityu {

using namespace std;

/**
 * \brief The class helps allocating unicast sessions to a network in order
 * to build up traffic volume.
 */
class TrafficAllocHelper
{
public:
  TrafficAllocHelper (void);
  ~TrafficAllocHelper (void);
  /**
   * \brief Allocate unicast traffic between nodes that are selected uniformly at random
   *
   * \param net The topology to which unicast sessions are allocated
   * \param helper The helper object to alocate unicast session between two nodes
   * \param nSessions The number of unicast sessions allocated to the network
   * \return A container of applications corresponding to the unicast sessions allocated
   */
  ApplicationContainer RandomAllocate (
    BaseTopology& net, Ptr<UnicastSessionHelper> helper, uint32_t nSessions);
  /**
   * \return All unicast session monitors corresponding to the unicast sessions just
   * allocated using RandomAllocate () method
   */
  list< Ptr<UnicastSessionMonitor> > GetMonitors ();
  /**
   * \return The maximum traffic rate (in Bps) allowed to allocate
   * on the network
   */
  double GetMaxTrafficBps () const;
  /**
   * \return The actual traffic rate (in Bps) now offering to the network 
   */
  double GetOfferedTrafficBps () const; 

private:
  // Store all the monitors for the unicast sessions that just allocated using RandomAllocate () method
  list< Ptr<UnicastSessionMonitor> > m_monitors;
  /// A random number generator
  UniformVariable m_rand;
  /// The sum of bidirectional bandwidth of all links
  double m_totalLinkBandwidth;
  /// The maximum number of unicast sessions to be allocated to a network. 
  uint32_t m_maxNSessions;
  /// The number of unicast sessions offered to a network in a simulation run
  uint32_t m_offeredNSessions;
  typedef list<Ptr<Application> > ApplicationList_t;
  /**
   * \brief The sourcing and sinking applications created by this helper
   * 
   * The sourcing and sinking applications are stored in the container in a way that 
   * m_applications[0] = source-1, m_applications[1] = sink-1,
   * m_applications[2] = source-2, m_applications[3] = sink-2, ...
   */
  ApplicationList_t m_applications;
  /// A helper for creating unicast session applications
  Ptr<UnicastSessionHelper> m_createSessionsHelper;
};

} // namespace eecityu
} // namespace ns3

#endif // #ifndef TRAFFIC_ALLOC_HELPER
